#include "widget.h"
#include "ui_widget.h"

#include <QPainter>
#include <QTimer>
#include <QtMath>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    setFixedSize(800,600);
    startSpeed();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::startSpeed()
{
    timer = new QTimer(this);
    currentValue = 0;
    startAngle = 150;

    connect(timer,&QTimer::timeout,[=](){

        if(!mark){
            currentValue++;
            if(currentValue >= 61)mark = 1;
        }
        if(mark){
            currentValue--;
            if(currentValue == 0)mark = 0;
        }

        update();
    });
    timer->start(50);
}

void Widget::drawElliInnerShine(QPainter &painter, int r)
{
    //画内渐变色圈
    QRadialGradient RadialGradient(0,0,r);
    RadialGradient.setColorAt(0,Qt::red);//中心颜色为黄色
    RadialGradient.setColorAt(1,Qt::black);//外围颜色为黑色
    painter.setBrush(RadialGradient);
    painter.drawEllipse(QPoint(0,0),r,r);
}

void Widget::drawOutShine(QPainter &painter, int r)
{
    //最外红圈
    //painter.restore();
    QRect rec(-r ,-r,r*2,r*2);
    painter.setPen(Qt::NoPen);

    QRadialGradient RadialGradient(0,0,r);
    RadialGradient.setColorAt(0,QColor(0,0,0,0));
    RadialGradient.setColorAt(0.92,QColor(0,0,0,0));
    RadialGradient.setColorAt(0.97,QColor(255,0,0,120));
    RadialGradient.setColorAt(1,Qt::red);

    painter.setBrush(RadialGradient);
    painter.drawPie(rec,(360-150)*16,-angle*61*16);
}

void Widget::drawLogo(QPainter &painter, int r)
{
    QRect rec(-100,r*0.5,200,60);
    painter.drawPixmap(rec,QPixmap(":/icon.png"));
}

void Widget::initCanvas(QPainter& painter){
    //初始化画布
    painter.setRenderHint(QPainter::Antialiasing, true);

    painter.setBrush(Qt::black);
    painter.drawRect(rect());

    QPoint cent(rect().width()/2,rect().height()*0.6);
    painter.translate(cent);
}

void Widget::drawMiddleCircle(QPainter &painter, int r)
{
    //画小圆
    painter.setPen(QPen(Qt::white, 3));
    painter.drawEllipse(QPoint(0, 0),r, r);
}

void Widget::drawCurrntSpeed(QPainter& painter)
{
    //画当前速度
    painter.setPen(Qt::white);
    QFont font("Arial",20);
    font.setBold(true);
    painter.setFont(font);
    painter.drawText(QRect(-60,-60,120,70),Qt::AlignCenter,QString::number(currentValue*4));

    QFont font2("Arial",15);
    font.setBold(true);
    painter.setFont(font2);
    painter.drawText(QRect(-60,-60,120,160),Qt::AlignCenter,"km/h");
}

void Widget::drawScale(QPainter &painter,int r)
{
    //画刻度线
    painter.save();//保存原点
    painter.setPen(QPen(Qt::white,5));
    painter.rotate(startAngle);
    for(int i = 0; i <= 60; i++){
        if(i >= 40)painter.setPen(QPen(Qt::red,5));
        if(i % 5 == 0){

            painter.drawLine(r-20,0,r-3,0);//画长刻度线
        }
        else painter.drawLine(r-10,0,r-3,0);//短刻度线
        painter.rotate(angle);
    }
    painter.restore();
}

void Widget::drawScaleDate(QPainter &painter)
{
    //画出刻度数字标识
    QFont font("Arial",12);
    font.setBold(true);
    painter.setFont(font);
    int r = height() / 2 - 46;
    for(int i = 0; i <= 60; i++){
        if(i%5==0){
            //保存坐标系
            painter.save();
            //算出平移点
            int delX = qCos(qDegreesToRadians(210 - angle * i)) * r;//Qt中sin认的是弧度
            int delY = qSin(qDegreesToRadians(210 - angle * i)) * r;
            //平移坐标系
            painter.translate(QPoint(delX, -delY));
            //旋转坐标系
            painter.rotate(-120+angle*i);
            //写上文字
            painter.drawText(-25,-25,50,30,Qt::AlignCenter,QString::number(i*4));
            //恢复坐标系
            painter.restore();
        }
    }
}

void Widget::drawPointLine(QPainter &painter, int len)
{
    //画指针

    painter.save();
    painter.setBrush(Qt::white);
    painter.setPen(Qt::NoPen);
    static const QPointF points[4] = {
        QPointF(0,0),QPointF(200,-1.1),QPointF(200,1.1),QPointF(0,15)
    };
    painter.rotate(startAngle + angle * currentValue);
    painter.drawPolygon(points,4);
    painter.restore();
    //painter.drawLine(len-55,0,60,0);
}

void Widget::drawSpeedPiw(QPainter &painter, int r)
{
    //画扇形
    //painter.restore();
    QRect rec(-r ,-r,r*2,r*2);
    painter.setPen(Qt::NoPen);
    painter.setBrush(QColor(255,0,0,80));
    painter.drawPie(rec,(360-startAngle)*16,-angle*currentValue*16);
}

void Widget::drawElliInnerBlack(QPainter &painter, int r)
{
    //画内黑圈
    painter.setBrush(Qt::black);
    painter.drawEllipse(QPoint(0,0),r,r);
}


void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    initCanvas(painter);//初始化画布
    drawMiddleCircle(painter,60);


    drawScale(painter,height()/2);
    drawScaleDate(painter);
    drawPointLine(painter, height()/2);
    drawSpeedPiw(painter, height()/2+25);
    drawElliInnerShine(painter, 140);
    drawElliInnerBlack(painter,80);
    drawCurrntSpeed(painter);
    drawOutShine(painter, height()/2+20);
    drawLogo(painter, height()/2);
}

